<template>
  <PageWrapper title="首页配置" v-loading="loading">
    <div class="py-8 bg-white flex flex-col justify-center items-center" style="padding: 16px">
      <BasicForm v-show="isEdit" @register="register" />
      <a-descriptions v-show="!isEdit" style="width: 100%" :column="1" bordered>
        <a-descriptions-item label="轮播图">
          <a-image-preview-group>
            <a-image
              v-for="(item, i) in configData.picUrlList"
              :key="i"
              :width="100"
              style="marginright: 10px"
              :src="item"
            />
          </a-image-preview-group>
        </a-descriptions-item>
        <a-descriptions-item label="视频">
          <video
            v-if="configData?.videoUrl"
            style="height: 200px"
            controls
            muted
            :src="configData.videoUrl"
          >
          </video>
        </a-descriptions-item>
      </a-descriptions>
      <br />
      <div v-if="!isEdit" class="flex justify-center">
        <a-button class="!ml-4" type="primary" @click="isEdit = true"> 修改 </a-button>
      </div>
      <div v-else class="flex justify-center">
        <a-button @click="isEdit = false"> 取消 </a-button>
        <a-button class="!ml-4" type="primary" @click="handleSubmit"> 确认 </a-button>
      </div>
    </div>
  </PageWrapper>
</template>
<script lang="ts" setup>
  import { onMounted, ref } from 'vue';
  import { PageWrapper } from '/@/components/Page';
  import { BasicForm, useForm } from '/@/components/Form';
  import { formSchema } from './index.data';
  import { getDetail, updateData } from '/@/api/homeCofig/index';
  import { CompanyItem } from '/@/api/aboutUs/model';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useRoute } from 'vue-router';
  const route = useRoute();
  const isEdit = ref(false); // 是否编辑状态
  const loading = ref(false);
  const configData = ref<CompanyItem>({});
  const { createMessage } = useMessage();
  const [register, { validate, setProps, updateSchema, setFieldsValue }] = useForm({
    size: 'large',
    // layout: 'vertical',
    labelWidth: 140,
    rowProps: {
      gutter: 24,
    },
    showActionButtonGroup: false,
    schemas: formSchema,
    disabled: !isEdit.value,
  });
  async function getConfigDetail() {
    try {
      loading.value = true;
      const res = await getDetail({
        zhCn: route.meta?.zhCn ?? 1,
      });
      isEdit.value = false;
      console.log(res);
      configData.value = res;
      setProps({
        model: { ...res, videoUrl: res.videoUrl ? [res.videoUrl] : '' },
      });
    } catch (error) {}
    loading.value = false;
  }
  async function handleSubmit() {
    try {
      loading.value = true;
      const values = await validate();
      await updateData({
        ...values,
        videoUrl: values.videoUrl[0],
        zhCn: route.meta?.zhCn ?? 1,
      });
      createMessage.success('修改成功');
      getConfigDetail();
    } catch (error) {}
    loading.value = false;
  }
  onMounted(() => {
    getConfigDetail();
  });
</script>

<style scoped>
  :deep(.slick-slide) {
    text-align: center;
    height: 160px;
    line-height: 160px;
    background: #364d79;
    overflow: hidden;
  }

  :deep(.slick-arrow.custom-slick-arrow) {
    width: 25px;
    height: 25px;
    font-size: 25px;
    color: #fff;
    background-color: rgba(31, 45, 61, 0.11);
    transition: ease all 0.3s;
    opacity: 0.3;
    z-index: 1;
  }
  :deep(.slick-arrow.custom-slick-arrow:before) {
    display: none;
  }
  :deep(.slick-arrow.custom-slick-arrow:hover) {
    color: #fff;
    opacity: 0.5;
  }

  :deep(.slick-slide h3) {
    color: #fff;
  }
</style>
